package com.zlsoft.web.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.huawei.hms.hmsscankit.ScanUtil;
import com.huawei.hms.ml.scan.HmsScan;
import com.qqz.common.utils.load.LoaderFactory;
import com.qqz.common.view.bar.CustomTitleBar;
import com.qqz.common.view.bar.OnTitleBarListener;
import com.zlsoft.mobilenurse.R;
import com.zlsoft.web.base.BaseActivity;
import com.zlsoft.web.sp.ConfigPreference;
import com.zlsoft.web.utils.QrUtils;
import com.zlsoft.web.utils.permissions.PermissionsUtils;

import java.io.IOException;


/**
 * 操作帮助
 *
 * @author - qqz
 * @date -2021-12-21
 * @note -
 * ---------------------------------------------------------------------------------------------------------------------
 * @modified -
 * @date -
 * @note -
 */
public class HelpActivity extends BaseActivity {


    private ImageView ivHelpA;

    private ImageView ivHelpB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_help);

        CustomTitleBar mViewById = findViewById(R.id.tb_title_lis);
        mViewById.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(CustomTitleBar mCustomTitleBar) {
                finish();
            }
        });
        initView();
    }

    /**
     * 回退到首页
     *
     * @param view
     */
    public void backToLaunch(View view) {
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == QrUtils.HW_SCAN_FOR_RESULT_LOCAL_CODE) {
            HmsScan obj = null;
            try {
                obj = data.getParcelableExtra(ScanUtil.RESULT);
            } catch (Exception e) {
                Toast.makeText(mActivity, "请重新尝试", Toast.LENGTH_SHORT).show();
            }
            if (null != obj) {
                String result = obj.originalValue;
                if (!result.contains("http")) {
                    Toast.makeText(mActivity, "该二维码不包含正确的地址信息.", Toast.LENGTH_LONG).show();

                    return;
                }
                try {
                    ConfigPreference.getInstance().setConfig(result);
                    Toast.makeText(mActivity, "配置成功", Toast.LENGTH_LONG).show();
                    startActivityEx(MainActivity.class);
                    finish();
                } catch (IOException mE) {
                    mE.printStackTrace();
                }
            }
        }
    }

    /**
     * 启动扫码
     *
     * @param view
     */
    public void startScan(View view) {
        try {
            if (ConfigPreference.getInstance().getConfig().equals("")) {
                PermissionsUtils.getInstance().getPermission(getContext(), mB -> {
                    QrUtils.getInstance().QrProduct(this, QrUtils.HW_SCAN_FOR_RESULT_LOCAL_CODE);
                });
            }
        } catch (IOException mE) {
            mE.printStackTrace();
            Toast.makeText(mActivity, "", Toast.LENGTH_SHORT).show();
        }
    }

    private void initView() {
        ivHelpA = (ImageView) findViewById(R.id.iv_help_a);
        ivHelpB = (ImageView) findViewById(R.id.iv_help_b);

        LoaderFactory.getILoader().loadResource(ivHelpA, R.drawable.ic_help_a);
        LoaderFactory.getILoader().loadResource(ivHelpB, R.drawable.ic_help_b);
    }
}
